package com.sharedprint.system.agent.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharedprint.system.agent.pojo.AgeUser;
import com.sharedprint.system.agent.service.AgeUserService;
import com.sharedprint.system.area.service.AreaService;
import com.sharedprint.system.organization.pojo.Organization;
import com.sharedprint.system.organization.service.OrganizationService;

@Controller
@RequestMapping("/api/ageuser")
public class AgeUserController {
private static final Logger logger=Logger.getLogger("AgeController");
@Autowired
private AgeUserService ageUserService;
private AreaService areaservice;
private OrganizationService orgservice;
@RequestMapping("/findPageList")
@ResponseBody
public Map<String, Object> queryPageList(HttpServletRequest req) {
	String token =req.getHeader("token");
	String userId=req.getHeader("userId");
	String currentPage = req.getParameter("currentPage");
	String pageSize = req.getParameter("pageSize");
	String queryAgeInfCode=req.getParameter("queryAgeInfCode");
	String queryAgeTrueNamLike=req.getParameter("queryAgeTrueNamLike");
	Map<String, Object> resultMap = new HashMap<String, Object>();
	logger.info("token=" + token + ",userId=" + userId);
	List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
	int totalCount = 0;

	resultMap.put("success", "0");
	if (token != null && !"".equals(token.trim()) && userId != null && !"".equals(userId.trim())) {
		if (currentPage == null || "".equals(currentPage.trim())) {
			currentPage = "0";
		}
		if (pageSize == null || "".equals(pageSize.trim())) {
			pageSize = "10";
		}
		Map<String, Object> searchMap = new HashMap<String, Object>();
		int currentPageIndex = Integer.parseInt(currentPage) * Integer.parseInt(pageSize);
		searchMap.put("currentPageIndex", currentPageIndex);
		searchMap.put("pageSize", Integer.parseInt(pageSize));
		searchMap.put("queryAgeInfCode", queryAgeInfCode);
		searchMap.put("queryAgeTrueNamLike", queryAgeTrueNamLike);
		try {
			totalCount = ageUserService.findCount(searchMap);
			if (totalCount > 0) {
			
				userList=ageUserService.findPageList(searchMap);
			}
			resultMap.put("currentPage", Integer.parseInt(currentPage) + 1);
			resultMap.put("totalCount", totalCount);
			resultMap.put("userList", userList);
			resultMap.put("success", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	return resultMap;
}

@RequestMapping("/delete")
@ResponseBody
private Map<String, Object> delete(HttpServletRequest req)  {
	Map<String, Object> result = new HashMap<String, Object>();
	int  AgeUser_ID = Integer.parseInt(req.getParameter("AgeUser_ID"));
	
	//Map<String, Object> searchMap = new HashMap<String, Object>();

	if (ageUserService.delete(AgeUser_ID)== 1) {
		result.put("success", "1");
	}
	else 
		result.put("success", "0");
	return result;
	

	
}
@RequestMapping("/resetupwd")
@ResponseBody
private Map<String, Object> updateUpwd(HttpServletRequest req)  {
	Map<String, Object> result = new HashMap<String, Object>();
	int  AgeUser_ID = Integer.parseInt(req.getParameter("AgeUser_ID"));
     
	if (ageUserService.resetLoginPwd(AgeUser_ID)== 1) {
		result.put("success", "1");
	}
	else 
		result.put("success", "0");
	return result;
	
}
@RequestMapping("/initAdd")
@ResponseBody
private Map<String, Object> getorg_areainfo(HttpServletRequest req){
	String token =req.getHeader("token");
	String userId=req.getHeader("userId");

	Map<String, Object> result = new HashMap<String, Object>();
	result.put("success", "0");
	if (token != null && !"".equals(token.trim()) && userId != null && !"".equals(userId.trim())) {
		List<Map<String, Object>> areaList = new ArrayList<Map<String, Object>>();
		List<Organization> orgList =  new ArrayList<Organization>();
		areaList=areaservice.findPageList(null);
		orgList=orgservice.findOrganizationList(null);
		result.put("areaList", areaList);
		result.put("orgList", orgList);
		result.put("success", "1");
	}
	return result;
}

}

